#ifndef MG811_H
#define MG811_H

#include <Arduino.h>
#include "macros.h"

/*******************Demo for MG-811 Gas Sensor Module V1.1*****************************
 * Author:  Tiequan Shao: tiequan.shao@sandboxelectronics.com
 * Peng Wei:     peng.wei@sandboxelectronics.com
 * 
 * Lisence: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
 * 
 * Note:    This piece of source code is supposed to be used as a demostration ONLY. More
 * sophisticated calibration is required for industrial field application. 
 * 
 * Sandbox Electronics    2012-05-31
 ************************************************************************************/

/************************Hardware Related Macros************************************/
#define DC_GAIN       8.5  //define the DC gain of amplifier



//Pines para los LED de nivel
#define LED1_D_PIN 12
#define LED2_D_PIN 11
#define LED3_D_PIN 10
#define LED4_D_PIN 9
#define LED5_D_PIN 8



/***********************Software Related Macros************************************/
#define SAMPLE_READ_INTERVAL 30        //define the time interval(in milisecond) between each samples in normal operation
#define MEASURE_SAMPLES 5             //define how many samples you are going to take for each measure (then average)

#define MIN_MEASURABLE_CO2_LEVEL 400
#define MAX_MEASURABLE_CO2_LEVEL 10000

#define MEASURE_PRECISSION 5.0         //Precisión con la que redondeamos las ppm medidas (para evitar medidas falsamente precisas)
#define ALARM_PRECISSION 50.0         //Precisión con la que redondeamos las ppm medidas (para evitar medidas falsamente precisas)

/**********************Application Related Macros**********************************/
//These two values differ from sensor to sensor. user should determine this value.
#define ZERO_POINT_VOLTAGE 0.306//0.317 //define the output of the sensor in volts when the concentration of CO2 is 400PPM (ORIGINAL VALUE 0.220)
                                 //Readings in volts from module are ZERO_POINT_VOLTAGE * DC_GAIN 
//#define _1000_PPM_VOLTAGE 0.37297
#define REACTION_VOLTAGE 0.020   //(ZERO_POINT_VOLTAGE - _1000_PPM_VOLTAGE)   //define the voltage drop of the sensor when move the sensor from air into 1000ppm CO2
                                                                    //(original value 0.020V)


#define LOWER_THAN_MIN_PPM -1
#define HIGHER_THAN_MAX_PPM 32000


class Co2SensorMg811
{
  public:
    Co2SensorMg811(int thresholdLevelPin, int co2SensorPin);
    void setup();
    
    int getThresholdLevel();
    void setLedStatus(int led1, int led2, int led3, int led4, int led5);
    int getPpm();
    
    double getMg811RawRead();
    double getHighestMg811VoltageRecorded() {return m_highestMg811VoltageRecorded;};
  
  private:
    void setupLevelLeds();
    int getMg811ppm(double volts, float *pcurve);
  
    double m_highestMg811VoltageRecorded;
    float  m_CO2Curve[3];
    
    int    m_thresholdLevelPin;
    int    m_co2SensorPin;
    
};



#endif
